package com.botarena.client.bot;

import java.util.ArrayList;
import java.util.List;

import com.botarena.client.UIHelper;
import com.botarena.client.bot.remote.BotAccess;
import com.botarena.client.bot.remote.BotAccessAsync;
import com.botarena.shared.BotInfo;
import com.botarena.shared.ContestInfo;
import com.botarena.shared.UserInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * GWT widget for listing/editing user's bots.
 */
public class BotEditor {
	
	private VerticalPanel panel;
	private VerticalPanel editFormPanel;
	private Panel newFormPanel;
	
	private List<BotInfo> botsList;
	
	private UserInfo userInfo;
	private ContestInfo contestInfo;
	
	public BotEditor(UserInfo userInfo, ContestInfo contestInfo) {
		this.userInfo = userInfo;
		this.contestInfo = contestInfo;
		
		botsList = new ArrayList<BotInfo>();
		
		editFormPanel = new VerticalPanel();
		getData();
		initPanel();
	}

	private void initPanel() {
		newFormPanel = createBotNewForm();
		
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.add(newComment("Create new bot:"));
		panel.add(newFormPanel);
		panel.add(newComment("Edit existing bots:"));
		panel.add(editFormPanel);
	}
	
	private Label newComment(String text){
		Label comment = new Label(text);
		comment.setStyleName("title5");
		return comment;
	}

	private void getData() {
		BotAccessAsync botService = GWT.create(BotAccess.class);
		botService.getMyBots(contestInfo.getKey(), new AsyncCallback<List<BotInfo>>() {
			
			@Override
			public void onSuccess(List<BotInfo> result) {
				botsList = result;
				renderEditFormPanel();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				editFormPanel.clear();
				editFormPanel.add(new Label("Sorry, unable to retrieve data..."));
			}
		});
		editFormPanel.clear();
		editFormPanel.add(UIHelper.createBigLoader("Retrieving data..."));
	}

	public Panel getPanel() {
		return panel;
	}
	
	private void renderEditFormPanel() {
		editFormPanel.clear();
		for (BotInfo bot : botsList) {
			BotEditFormWidget botWidget = new BotEditFormWidget(this, bot);
			editFormPanel.add(botWidget.getPanel());
		}
	}
	
	private Panel createBotNewForm() {
		BotNewFormWidget newForm = new BotNewFormWidget(this, contestInfo);
		return newForm.getPanel();
	}
	
	public void addNewBot(String botKey) {
		final BotEditor botEditor = this;
		BotAccessAsync botService = GWT.create(BotAccess.class);
		botService.getByKey(botKey, new AsyncCallback<BotInfo>() {
			
			@Override
			public void onSuccess(BotInfo result) {
				botsList.add(0, result);
				BotEditFormWidget botWidget = new BotEditFormWidget(botEditor, result);
				editFormPanel.insert(botWidget.getPanel(), 0);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				editFormPanel.add(new Label("Sorry, unable to retrieve data..."));
			}
		});
	}
	
	public void updateBot(BotInfo botInfo) {
		for (int i = 0; i < botsList.size(); i++) {
			if (botsList.get(i).getKey().equals(botInfo.getKey())) {
				botsList.set(i, botInfo);
				break;
			}
		}
	}
	
	public void deleteBot(BotInfo botInfo) {
		botsList.remove(botInfo);
		renderEditFormPanel();
	}
}
